package com.linsy.product.redis.mq.service;

import com.linsy.product.redis.mq.entity.MqDataEntity;
import com.linsy.product.redis.mq.entity.PutMsgDTO;

import java.util.List;

/**
 * 发送者装饰器
 */
public abstract class SendMsgDecorator<T> implements RedisMqSend<T> {

    /**
     * 消费主题
     */
    private final String key;
    private final Broker broker;

    public SendMsgDecorator(Broker broker, String key) {
        this.broker = broker;
        this.key = key;
    }

    /**
     * 发送消息到redis队列
     */
    @Override
    public void sendMsg(T t) {
        PutMsgDTO dto = new PutMsgDTO();
        dto.setValue(convert(t));
        dto.setKey(key);
        List<MqDataEntity> entityList = MqDataEntity.toEntityList(dto);
        beforeSend(entityList);
        broker.pullType(dto.getKey(), entityList);
        afterSend(entityList);
    }

}
